package csproj.fencing;

/*   
 *   Referee:
 *      A mediator between two players. It is responsible to initialize 
 *      the players and pass the plays between them and terminates the game. 
 *      It is the responsibility of the players to check whether they have won and
 *      maintain the board state.
 *
 *   @author msalehi
 */


public class Referee implements Piece{

	private static Player P1;
	private static Player P2;

    /*
      * Input arguments: first board size, second path of player1 and third path of player2
      */
	public static void main(String[] args)
	{
        Move lastPlayedMove = new Move();
		boolean None= true;
		int NumberofPiece=0;
		int NumberofBarrier=0;
		int NumberofMoves=0;
		int BoardSize = Integer.valueOf(args[0]);
		System.out.println("Referee started !");
		try{
			P1 = (Player)(Class.forName(args[1]).newInstance());
			P2 = (Player)(Class.forName(args[2]).newInstance());
		}
		catch(Exception e){
			System.out.println("Error "+ e.getMessage());
			System.exit(1);
		}

		P1.init(Integer.valueOf(args[0]), WHITE);
		P2.init(Integer.valueOf(args[0]), BLACK);

		while(None && NumberofPiece<=(BoardSize*BoardSize) && NumberofBarrier<((BoardSize-1)*2*BoardSize))
		{
			lastPlayedMove =P1.makeMove();
			NumberofMoves++;
			P1.printBoard(System.out);
			if (lastPlayedMove.HasPiece)
			{
				NumberofPiece++;
				System.out.println("Putting BoardPiece "+ lastPlayedMove.PieceRow+":"+ lastPlayedMove.PieceCol+" by "+ lastPlayedMove.P);
			}
			if (lastPlayedMove.HasBarrier)
			{
				NumberofBarrier++;
				System.out.println("Putting Barrier "+ lastPlayedMove.Direction+":"+ lastPlayedMove.BarrierRow+":"+ lastPlayedMove.BarrierCol+" by "+ lastPlayedMove.P);
			}
			if(P2.opponentMove(lastPlayedMove)<0)
			{
				System.out.println("Exception: Player 2 rejected the move of player 1.");
				P1.printBoard(System.out);
				P2.printBoard(System.out);
				System.exit(1);
			}
			if (P2.getWinner()!=-1)
				None=false;
			else
			{
				lastPlayedMove = P2.makeMove();
				NumberofMoves++;
				P2.printBoard(System.out);
				if (lastPlayedMove.HasPiece)
				{
					NumberofPiece++;
					System.out.println("Putting BoardPiece "+ lastPlayedMove.PieceRow+":"+ lastPlayedMove.PieceCol+" by "+ lastPlayedMove.P);
				}
				if (lastPlayedMove.HasBarrier)
				{
					NumberofBarrier++;
					System.out.println("Putting Barrier "+ lastPlayedMove.Direction+":"+ lastPlayedMove.BarrierRow+":"+ lastPlayedMove.BarrierCol+" by "+ lastPlayedMove.P);
				}
				if(P1.opponentMove(lastPlayedMove)<0)
				{
					System.out.println("Exception: Player 1 rejected the move of player 2.");
					P2.printBoard(System.out);
					P1.printBoard(System.out);
					System.exit(1);
				}
				if (P1.getWinner()!=-1)
					None=false;				

			}
		}
		System.out.println("P2 Board is :");
		P2.printBoard(System.out);
		System.out.println("P1 Board is :");
		P1.printBoard(System.out);

		System.out.println("Player one (White) indicate winner as: "+ P1.getWinner());
		System.out.println("Player two (Black) indicate winner as: "+ P2.getWinner());
		System.out.println("Total Number of Moves Played in the Game: "+ NumberofMoves);
		System.out.println("Referee Finished !");
	}


}
